package com.wen.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.wen.yygh.cmn.client.DictFeignClient;
import com.wen.yygh.hosp.repository.HospitalRepository;
import com.wen.yygh.hosp.service.HospitalService;
import com.wen.yygh.model.hosp.Hospital;
import com.wen.yygh.vo.hosp.HospitalQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Wenjl
 */
@Service
@Slf4j
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> paramMap) {
        //把获取到的信息转为字符串
        String mapString = JSONObject.toJSONString(paramMap);
        //将字符串转换位对象
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        String hoscode = hospital.getHoscode();
        //判断是否存在数据
        Hospital hospitalExist = hospitalRepository.findHospitalByHoscode(hoscode);
        //如果存在则修改
        if (hospitalExist != null) {
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);

        } else {
            //如果不存在则添加
            hospital.setIsDeleted(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setStatus(0);

        }
        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital getHospitalByHoscode(String hoscode) {

        return hospitalRepository.findHospitalByHoscode(hoscode);
    }

    @Override
    public Page<Hospital> findHospPageByHospitalQueryVo(Integer pageNum, Integer pageSize, HospitalQueryVo hospitalQueryVo) {
        //Example对象模糊查询
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        //创建Hospital对象
        Hospital hospital = new Hospital();
        if (hospitalQueryVo != null) {
            BeanUtils.copyProperties(hospitalQueryVo, hospital);
        }
        Example example = Example.of(hospital, matcher);
        //创建排序对象,根据创建实现倒叙输出
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        //创建Pageable对象
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize, sort);
        Page<Hospital> page = hospitalRepository.findAll(example, pageable);
        List<Hospital> content = page.getContent();
        //获取查询list结合，便利进行医院等级封装
        for (Hospital item : content) {
            this.setHospitalHosType(item);
        }


        return page;
    }

    @Override
    public void updateStatusById(String id, Integer status) {


        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Hospital findHospitalById(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        this.setHospitalHosType(hospital);
        return hospital;

    }

    @Override
    public String findHospName(String hoscode) {
        Hospital hospital = hospitalRepository.findHospitalByHoscode(hoscode);

        return hospital != null ? hospital.getHosname() : "";
    }

    @Override
    public List<Hospital> findHospByHospName(String hosname) {
        return hospitalRepository.findHospByHosnameLike(hosname);
    }

    /**
     * 根据医院编号获取医院详情
     *
     * @param hoscode
     * @return
     */
    @Override
    public Map<String, Object> getParticularsByHoscode(String hoscode) {
        //获取医院详细信息
        Hospital hospital = this.setHospitalHosType(this.getHospitalByHoscode(hoscode));
        //最终返回结果
        Map<String, Object> result = new HashMap<>();
        //医院详情
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    private Hospital setHospitalHosType(Hospital hospital) {
        log.info(hospital.getHostype());
        //根据dictCode和value获取医院等级名称
        String hostype = dictFeignClient.getName("Hostype", hospital.getHostype());
        log.info("hostype=>" + hostype);
        //查询省 市 地区
        String provinceCode = dictFeignClient.getName(hospital.getProvinceCode());
        String cityCode = dictFeignClient.getName(hospital.getCityCode());
        String districtCode = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("hostype", hostype);
        hospital.getParam().put("provinceCode", provinceCode);
        hospital.getParam().put("cityCode", cityCode);
        hospital.getParam().put("districtCode", districtCode);
        hospital.getParam().put("fullAddress", provinceCode + cityCode + districtCode);

        return hospital;
    }
}
